<?php 

//require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';

?>

<div id="addstats"> 

<div class="container-fluid">
<div class="row">
<div class="col-lg-3 col-md-3"></div>
<div class="col-lg-6 col-md-6"> 
<h2>Add Stats to Database by Quarter</h2>

<form method="post" action="" id="form1"> 
SELECT season:<br/>
 <select name="season" class="form-control" id="select">

 <?php if (isset($_POST['season'])) {

 $season = $_POST['season'];
 	echo "<option id=\"selected-season\" value=\"$season\" selected=\"selected\">$season is selected</option>";
 	
 } else {
 	echo "<option id=\"selected-season\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a season</option>";
 }

 ?>
 
         
<?php

$sql = "SELECT DISTINCT season FROM games ORDER BY season";

$result = $db->query ($sql);

while ($row = $result->fetch_assoc()) {

  if ($row['season'] >= '2018/2019') {
    
    $season = $row['season'];
    echo "<option value=\"$season\"> $season </option>";
  }
  
}
?>
  </select>
<br/>


<?php if (isset($_POST['season'])) { 

$season = $_POST['season']; ?>

SELECT Game: <br/>

<select name="gamenumber" class="form-control" id="select">

<?php if (isset($_POST['gamenumber'])) {

 	$season = $_POST['season'];
 	$gamenumber = $_POST['gamenumber'];
 	
 	echo "<option id=\"selected-game\" value=\"$gamenumber\" selected=\"selected\">Game $gamenumber is selected</option>";
 	
 } else {
 	echo "<option id=\"selected-game\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a game</option>";
 } ?>

<?php 
$sql = "SELECT gamenumber, date, opponent, location
FROM games
WHERE season = '$season'
ORDER BY date DESC";

$result=$db->query($sql);

while($row=$result->fetch_assoc()){ 

    $gamenumber = $row['gamenumber'];
    $date = $row['date'];
    $opponent = $row['opponent'];
    $location = $row['location'];

  echo "<option value='$gamenumber'>Game $gamenumber $date ($opponent $location)  </option>";
}

?>

</select>

<?php } ?>

<br/>

<?php if (isset($_POST['season']) && isset($_POST['gamenumber']) ) {  

$season = $_POST['season'];
$gamenumber = $_POST['gamenumber'];

$sql = "SELECT opponent
FROM games
WHERE season = '$season' and gamenumber = '$gamenumber'";

$result=$db->query($sql);
$row=$result->fetch_assoc(); 
$opponent = $row['opponent'];

$sql = "SELECT DISTINCT teamname
FROM teams
WHERE season = '$season'";

$result=$db->query($sql);
$row=$result->fetch_assoc(); 
$coachteam = $row['teamname'];

	?>

SELECT Team:<br/>

<select name="team" class="form-control" id="select">

<?php if (isset($_POST['team'])) {

  if ($_POST['team'] == $opponent) {
      
  	$team_sel = $_POST['team'];
  } else {
      $team_sel = $coachteam;
  }
 	
 	echo "<option id=\"selected-game\" value=\"$team_sel\" selected=\"selected\"> $team_sel is selected</option>";
 	
 } else {
 	echo "<option id=\"selected-game\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a team</option>";
 } ?>

	<option value="coachteam"><?= $coachteam; ?></option>
	<option value="<?= $opponent; ?>"><?= $opponent; ?></option>

</select>


<?php } ?>

<br/>

<?php if ( isset($_POST['season']) && isset($_POST['gamenumber']) && isset($_POST['team']) ) {  ?>

SELECT Quarter:<br/>

<select name="quarter" class="form-control" id="select">

<?php if (isset($_POST['quarter'])) {

 	$quarter = $_POST['quarter'];
 	
 	echo "<option id=\"selected-quarter\" value=\"$quarter\" selected=\"selected\"> Q$quarter is selected</option>";
 	
 } else {
 	echo "<option id=\"selected-quarter\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a quarter</option>";
 } ?>

		<option value="1">Q1</option>
		<option value="2">Q2</option>
		<option value="3">Q3</option>
		<option value="4">Q4</option>
		<option value="OT">OT</option>

</select>

<?php }?>

<br/>

<?php if (isset($_POST['season']) && isset($_POST['gamenumber']) && isset($_POST['team']) && isset($_POST['quarter']) ) {  

	$season = $_POST['season'];
	$gamenumber = $_POST['gamenumber'];
	$team = $_POST['team'];
	$quarter = $_POST['quarter'];

  
if ($team == $opponent) {

   $sql = "SELECT teams_opponents.playerID, users.firstname, users.lastname, teams_opponents.playernumber
FROM users 
JOIN teams_opponents
ON users.user_key = teams_opponents.playerID
WHERE (users.type = 'Player'
OR users.type = 'Team')
AND teams_opponents.season = '$season'
AND teams_opponents.teamname = '$team'
ORDER BY teams_opponents.playernumber ASC";

} else {

  $sql = "SELECT users.user_key, users.firstname, users.lastname, teams.playernumber
FROM users 
JOIN teams
ON users.user_key = teams.playerID
WHERE (users.type = 'Player'
OR users.type = 'Team')
AND teams.season = '$season'
ORDER BY teams.playernumber ASC";

}

  $result=$db->query($sql);
  
	?>

  SELECT player:<br/>

<select name="playerID" class="form-control" id="select">

<?php if (isset($_POST['playerID'])) {

  $team = $_POST['team'];

  $playerID = $_POST['playerID'];

  if ($team == $opponent) {

$sql2 = "SELECT users.firstname, users.lastname, teams_opponents.playernumber
  FROM users 
  JOIN teams_opponents
  ON users.user_key = teams_opponents.playerID 
  WHERE users.user_key = '$playerID'";
 

  } else {
    

   $sql2 = "SELECT users.firstname, users.lastname, teams.playernumber
  FROM users 
  JOIN teams
  ON users.user_key = teams.playerID 
  WHERE users.user_key = '$playerID'";

  }

  $result2=$db->query($sql2);
  $row2=$result2->fetch_assoc();

  $playernumber_selected = $row2['playernumber'];
  $firstname_selected = $row2['firstname'];
  $lastname_selected = $row2['lastname'];

  
  echo "<option id=\"selected-playernumber\" value=\"$playerID\" selected=\"selected\"> $playernumber_selected - $firstname_selected $lastname_selected is selected</option>";
  
 } else {
  echo "<option id=\"selected-playernumber\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a playernumber</option>";
 }

 if ($team == $opponent) {

 while( $row=$result->fetch_assoc() ){ 

    $playerID = $row['playerID'];
    $playernumber = $row['playernumber'];
    $firstname = $row['firstname'];
    $lastname = $row['lastname'];

    echo "<option value='$playerID'>$playernumber - $firstname $lastname </option>";

    }

  } else {


    while( $row=$result->fetch_assoc() ){ 

    $user_key=$row['user_key']; 
    $lastname=$row['lastname']; 
    $firstname=$row['firstname']; 
    $type=$row['type'];
    $playernumber = $row['playernumber'];

    echo "<option value='$user_key'>$playernumber - $firstname $lastname</option>";

  }
}

  ?>


</select>
<br/>
</form>

<?php }?>

<?php if (isset($_POST['season']) && isset($_POST['gamenumber']) && isset($_POST['team']) && isset($_POST['quarter']) && isset($_POST['playerID']) ) {  

  $season = $_POST['season'];
  $gamenumber = $_POST['gamenumber'];
  $team = $_POST['team'];
  $quarter = $_POST['quarter'];
  $playerID = $_POST['playerID'];

  ?>

  <form method="post" action="" id="form2">

     <input type="hidden" name="season" value="<?= $season; ?>"/>
    <input type="hidden" name="gamenumber" value="<?= $gamenumber; ?>"/>
    <input type="hidden" name="quarter" value="<?= $quarter; ?>"/>
     <input type="hidden" name="playerID" value="<?= $playerID; ?>"/>




    <div class="form-group">        
      <div class="col-lg-offset-3 col-md-offset-3 col-lg-6 col-md-6">
        <button type="reset" class="btn btn-lg btn-danger btn-block">RESET FORM</button> 
      </div>
      <br><br>
    </div>

<?php  if ($quarter == '1') {
  
      ?>


<div class="form-group">
  <label for="ex3">Starting 5:</label>
        <select class="form-control" name="S5">
          <option value="yes">yes</option>
          <option value="no" selected="selected">no</option>
        </select>
</div>
<br/>


<?php } ?>

<div class="input-group">
        <input class="form-control" id="ex3" type="text" placeholder="Minutes Played 00:00" name="MP">
        <span class="input-group-addon">MP</span>
</div>
<br/>
    
      <div class="input-group">
        <input class="form-control" id="ex1" type="number" placeholder="Defensive Rebounds" name="DRB">
       <span class="input-group-addon">DRB</span>
      </div>
      <br/>
      <div class="input-group">
        <input class="form-control" id="ex2" type="number" placeholder="Offensive Rebounds" name="ORB">
        <span class="input-group-addon">ORB</span>
      </div>
      <br/>
     <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="Turnovers" name="TOV">
        <span class="input-group-addon">TO</span>
      </div>
      <br/>
      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="Steals" name="ST">
        <span class="input-group-addon">ST</span>
      </div>
      <br/>

  <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="Assists" name="AST">
        <span class="input-group-addon">AST</span>
      </div>
      <br/>

      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="Blocked Shots" name="BS">
        <span class="input-group-addon">BS</span>
      </div>
      <br/>

      
         
      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="FG Inside scored" name="FGIscored">
        <span class="input-group-addon">FGI scored
          
        </span>
      </div>
      <br/>
      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="FG Inside total attemps" name="FGItotal">
        <span class="input-group-addon">FGI total
        </span>
      </div>
      <br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="FG Outside scored" name="FGOscored">
        <span class="input-group-addon">FGO scored</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="FG Outside total attempts" name="FGOtotal">
        <span class="input-group-addon">FGO total</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="3P scored" name="THPscored">
        <span class="input-group-addon">3P scored</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="3P total attempts" name="THPtotal">
        <span class="input-group-addon">3P total</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="Free Throws scored" name="FTscored">
        <span class="input-group-addon">FT scored</span>
      </div>
      <br/>
      <div class="input-group">
       <input class="form-control" id="ex3" type="number" placeholder="Free Throws total attempts" name="FTtotal">
         <span class="input-group-addon">FT total</span>
      </div>
      <br/>


 <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="Personal Fouls" name="PF">
        <span class="input-group-addon">PF</span>
      </div>
      <br/>
   
       <div id="addstatshandler3content"> </div>
   

    <div class="form-group">        
      <div class="col-lg-offset-4 col-md-offset-4 col-lg-4 col-md-4">
        <button type="submit" class="btn btn-lg btn-success btn-block" id="addstatssubmit"> Add</button>
      </div>
    </div>

<br>

   </form>


<?php }?>


<br>
</div>
<div class="col-lg-3 col-md-3"></div>
</div>
</div>


</div>

<script type="text/javascript">
	
$("#form1 select").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./addstats_byquarter.php",
        data: postData,
        success: function(data) { 
            $("#addstats").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#addstats").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});


$("#addstatssubmit").on("click", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
      type: "POST",
      url: "./includes/handlers/addstats_byquarter_handler.php",
      data: postData,
      success: function(data) {
        $("#addstatshandler3content").html(data);
      },
        error: function (xhr, status, error) {
          $("#addstatshandler3content").html(xhr.responseText); 
        }
  }); 
  e.preventDefault();
});

</script>
